package com.ruoyi.fztz.tbzn.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.fztz.tbzn.domain.Tbzn;
import com.ruoyi.fztz.tbzn.domain.TbznFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.fztz.tbzn.service.ITbznService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 填报指南Controller
 *
 * @author huacai
 * @date 2024-11-29
 */
@RestController
@RequestMapping("/fztz/tbzn")
public class TbznController extends BaseController
{
    @Autowired
    private ITbznService tbznService;

    /**
     * 查询填报指南列表
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tbzn tbzn)
    {
        startPage();
        List<Tbzn> list = tbznService.selectTbznList(tbzn);
        return getDataTable(list);
    }

    /**
     * 导出填报指南列表
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:export')")
    @Log(title = "填报指南", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tbzn tbzn)
    {
        List<Tbzn> list = tbznService.selectTbznList(tbzn);
        ExcelUtil<Tbzn> util = new ExcelUtil<Tbzn>(Tbzn.class);
        util.exportExcel(response, list, "填报指南数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Tbzn> util = new ExcelUtil<Tbzn>(Tbzn.class);
        util.importTemplateExcel(response, "填报指南数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "填报指南", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Tbzn> util = new ExcelUtil<Tbzn>(Tbzn.class);
        InputStream inputStream = file.getInputStream();
        List<Tbzn> list = util.importExcel(inputStream );
        inputStream.close();
        int count = tbznService.batchInsertTbzn(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取填报指南详细信息
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:query')")
    @GetMapping(value = "/{tbznId}")
    public AjaxResult getInfo(@PathVariable("tbznId") String tbznId)
    {
        return success(tbznService.selectTbznByTbznId(tbznId));
    }

    /**
     * 新增填报指南
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:add')")
    @Log(title = "填报指南", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tbzn tbzn)
    {
        tbzn.setUserId(Math.toIntExact(getUserId())); //用户ID
        tbzn.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        tbzn.setCreateBy(getUsername()); //创建人
        return toAjax(tbznService.insertTbzn(tbzn));
    }

    /**
     * 修改填报指南
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:edit')")
    @Log(title = "填报指南", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tbzn tbzn)
    {
        return toAjax(tbznService.updateTbzn(tbzn));
    }

    /**
     * 删除填报指南
     */
    @PreAuthorize("@ss.hasPermi('fztz:tbzn:remove')")
    @Log(title = "填报指南", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tbznIds}")
    public AjaxResult remove(@PathVariable String[] tbznIds)
    {
        return toAjax(tbznService.deleteTbznByTbznIds(tbznIds));
    }

    /**
     * 获得执行填报主键ID (UUID)
     */
    @GetMapping("/getTbznId")
    public AjaxResult getTbznId(){
        String zcJd = IdUtils.fastSimpleUUID();
        return success(zcJd);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertTbznFjByfztzId(@RequestBody TbznFj tbznFj){
        return toAjax(tbznService.insertTbznFjByfztzId(tbznFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectTbznFjList(String sId){
        TbznFj tbznFj = new TbznFj();
        tbznFj.setTbznId(sId);
        return AjaxResult.success(tbznService.selectTbznFjList(tbznFj));
    }

    /**
     * 删除填报指南附件
     */
    @Log(title = "填报指南附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(tbznService.deleteTbznFjByFjIds(fjIds));
    }
}
